<template>
  <view class="institution">
    <view class="inputBox flex-center-between">
      <view class="inputName">
        联系人
      </view>
      <u-input maxlength="20" :disabled="disabled" placeholder="请填写联系人" :border="border" :customStyle="inputStyle" inputAlign="right" v-model="formData.name" clearable></u-input>
    </view>
    <view class="inputBox flex-center-between">
      <view class="inputName">
        电话
      </view>
      <u-input :disabled="disabled" placeholder="请填写电话" type="number" maxlength="11" :border="border" :customStyle="inputStyle" inputAlign="right" v-model="formData.phone" clearable></u-input>
    </view>
    <view class="inputBox flex-center-between">
      <view class="inputName">
        价格
      </view>
      <view class="flex-center-between">
        <u-input maxlength="8" placeholder="请填写出价乐器价格" :disabled="disabled" type="number" :border="border" :customStyle="inputStyle" inputAlign="right" v-model="formData.price" clearable></u-input>
        <text style="font-size: 28rpx;font-family: PingFang SC-Bold, PingFang SC;font-weight: bold;color: #FF2544;margin-left: 8rpx;">元</text>
      </view>
    </view>
    <view class="inputBox flex-center-between">
      <view class="inputName">
        地址
      </view>
      <u-input maxlength="50" :disabled="disabled" placeholder="请填写地址" :border="border" :customStyle="inputStyle" inputAlign="right" v-model="formData.address" clearable></u-input>
    </view>

    <view class="illustrate">
      <text class="name">
        留言
      </text>
      <view class="textareaBox">
        <u-input :disabled="disabled" :customStyle="inputStyle" :auto-height="false" class="textarea" placeholder="请填写留言" type="textarea" v-model="formData.main_content" clearable></u-input>
      </view>
    </view>
    <view class="botton-st" @click.stop="getdevAskgoodsPriceInsert()" v-if="!disabled">
      确认出价
    </view>
  </view>
</template>

<script>
import api from '@/api/index.js'
export default {

  data() {
    return {
      safeAreaBottom: uni.getStorageSync('safeAreaBottom'),
      border: false,
      formData: {
        id: '',
        name: '',
        phone: '',
        price: '',
        address: '',
        main_content: ''
      },
      inputStyle: {
        'width': '50%',
        'font-size': '28rpx',
        'color': '#999999'
      },
      disabled: true,
      isCanRequest: true
    }
  },
  onLoad(option) {
    console.log(option)
    if (option.disabled == 'false') {
      this.disabled = false
      this.formData.id = option.id
      uni.setNavigationBarTitle({
        title: '出价'
      });
    } else if (option.disabled == 'true') {
      this.disabled = true
      this.getdevAskgoodsPriceDetail(option.id)
    }
    // uni.hideTabBar();

  },
  methods: {
    setIsRequestFlag() {
      if (this.isCanRequest) {
        this.isCanRequest = false
        setTimeout(() => {
          this.isCanRequest = true
        }, 5000)
        return true
      } else {
        return false
      }
    },
    getdevAskgoodsPriceDetail(id) {
      api.base.getdevAskgoodsPriceDetail({ id: id }).then(res => {
        if (res.code == 1) {
          this.formData = res.data
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      });
    },
    getdevAskgoodsPriceInsert() {
      const phoneReg = /^1[34578]\d{9}/;
      let showText = '';
      if (!this.formData.name) {
        showText = '请填写联系人'
      } else if (!this.formData.phone) {
        showText = '请填写电话'
      } else if (!phoneReg.test(this.formData.phone)) {
        showText = '请填写有效电话'
      } else if (!this.formData.address) {
        showText = '请填写地址'
      } else if (!this.formData.main_content) {
        showText = '请填写留言'
      }

      if (showText != '') {
        uni.$u.toast(showText);
        return
      }
      let flag = this.setIsRequestFlag()
      if (!flag) {
        console.log('不让请求')
        return
      } else {
        api.base.getdevAskgoodsPriceInsert(this.formData).then(res => {
          if (res.code == 1) {
            uni.$u.toast("报价成功")
            setTimeout(() => {
              uni.navigateBack();
            }, 1000)
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        });
      }

    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../my/my.scss";

.institution {
  padding-top: 24rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  .botton-st {
    height: 88rpx;
    background: #6d0cf7;
    border-radius: 24rpx 24rpx 24rpx 24rpx;
    opacity: 0.5;
    font-size: 28rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: #ffffff;
    line-height: 88rpx;
    text-align: center;
    margin: 32rpx 64rpx 0rpx 64rpx;
    width: calc(100% - 128rpx);
  }
}
</style>